package com.xiaoyi.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.support.v7.widget.RecyclerView;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.avin.yi.R;
import com.xiaoyi.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;


public class ColorTypeAdapter extends RecyclerView.Adapter<ColorTypeAdapter.MyViewHolder> {
    private LayoutInflater mInflater;
    private Context mContext;
    private List<String> mItems = new ArrayList<>();


    private OnItemSelect onItemSelect;


    public ColorTypeAdapter(Context context) {
        mInflater = LayoutInflater.from(context);
        mContext = context;
    }

    public void setDatas(List<String> items) {
        mItems.clear();
        mItems.addAll(items);
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.item_colorselect, parent, false);
        final MyViewHolder holder = new MyViewHolder(view);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                if (onItemSelect != null) {
                    onItemSelect.onItemClick(position, mItems.get(position));
                }
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        GradientDrawable myGrad = (GradientDrawable) holder.value.getBackground();
        myGrad.setColor(Color.parseColor(mItems.get(position)));
    }

    @Override
    public int getItemCount() {
        return mItems.size();
    }



    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView value;

        public MyViewHolder(View itemView) {
            super(itemView);
            value = itemView.findViewById(R.id.value);
        }
    }

    public void setOnItemSelect(OnItemSelect itemClickListener) {
        this.onItemSelect = itemClickListener;
    }

    public interface OnItemSelect {
        void onItemClick(int position  ,String colorStr);
    }
}
